package com.etb.wisdom.eframe.dict.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.etb.core.pojo.CriteriaType;
import com.etb.core.pojo.Page;
import com.etb.web.action.ETBAction;
import com.etb.web.eui.utils.EuiUtil;
import com.etb.wisdom.eframe.dict.service.EtbDictinfoService;
import com.etb.wisdom.eframe.dict.vo.DictTreeGridNode;
import com.etb.wisdom.eframe.model.EtbDictinfo;
import com.etb.wisdom.eframe.model.EtbDicttype;

@Action("info")
@Namespace("/eframe/dict/info")
public class EtbDictinfoAction extends ETBAction<EtbDictinfo> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4963648261510552052L;
	
	@Autowired
	private EtbDictinfoService infoService;

	@Override
	public String addView() {
		if(StringUtils.isNotBlank(id)) {
			EtbDictinfo uplevelInfo = new EtbDictinfo();
			uplevelInfo.setId(id);
			service.extend(uplevelInfo);
			request.setAttribute("uplevelInfo", uplevelInfo);
		}
		String typeId = request.getParameter("typeId");
		if(StringUtils.isNotBlank(typeId)) {
			EtbDicttype type = new EtbDicttype();
			type.setId(typeId);
			service.extend(type);
			request.setAttribute("type", type);
		}
		return ADD;
	}
	
	@Override
	public String updateView() {
		super.updateView();
		if(dataObject.getUplevelid() != null) {
			request.setAttribute("uplevelInfo", infoService.getUplevelInfo(dataObject));
		}
		EtbDicttype type = new EtbDicttype();
		type.setId(dataObject.getDicttypeid());
		service.extend(type);
		request.setAttribute("type", type);
		return UPDATE;
	}
	
	@Override
	public void save() {
		infoService.save(dataObject);
	}
	
	@Override
	public void list() {
		if(page == null) page = new Page();
		if(criteria == null) criteria = new CriteriaType();
		criteria.setEntity(EtbDictinfo.class.getName());
		String typeId = request.getParameter("typeId");
		//id!=null：获取子节点所有数据
		//StringUtils.isNotBlank(typeId)：获取指定字典类型的顶级字典信息
		//criteria.getExpr().size() == 0：获取所有顶级字典信息
		if(id != null) {
			criteria.getExpr().clear();
			criteria.addExpr("uplevelid_eq", id);
		} else if(StringUtils.isNotBlank(typeId)) {
			criteria.addExpr("dicttypeid_eq", typeId);
		} else if(criteria.getExpr().size() == 0) {
			criteria.addExpr("uplevelid_eq", null);
		}
		
		page.init();
		List<DictTreeGridNode> treeData = new ArrayList<DictTreeGridNode>();
		List<EtbDictinfo> infos = service.query(criteria, page);
		for(EtbDictinfo info: infos) {
			DictTreeGridNode node = new DictTreeGridNode(info.getId(), info.getInfoname(), info.getInfocode());
			node.setSortno(info.getSortno());
			if(id != null) {
				node.set_parentId(info.getUplevelid());
			}
			node.setState(DictTreeGridNode.State.closed.toString());
			treeData.add(node);
		}
		EuiUtil.toGrid(treeData, page);
	}
	
}